package cn.fl.infside.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import cmm.mid.core.framework.page.Pager;
import cmm.user.facade.system.api.ISysOrgApiService;
import cmm.user.facade.system.dto.SysOrgDTO;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.infside.service.IInfBfsPayService;
import cn.fl.infside.transform.InfBfsPayVOTransform;
import cn.fl.infside.vo.InfBfsPayVO;
import fl.inf.facade.api.IBFSApiService;
import fl.inf.facade.condition.InfBfsPayQueryCondition;
import fl.inf.facade.dto.InfBfsPayDTO;
import fl.inf.facade.dto.MessageDTO;
import ins.car.facade.car.prj.api.InsOrPrjCombinationApiService;

/**
 * @Description: 资金系统支付接口表 客户端服务层接口实现
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Service
public class InfBfsPayServiceImpl implements IInfBfsPayService {

	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(IInfBfsPayService.class);
	
	/**
	 * TODO 自定义 API 暴露服务
	 */
	//@Autowired
	//private I自定义ApiService 自定义ApiService;

	@Autowired
	private IBFSApiService bFSApiService;

	/**
	 * 组织架构 API 暴露服务
	 */
	@Autowired
	private ISysOrgApiService sysOrgApiService;
	@Autowired
	private InsOrPrjCombinationApiService insOrPrjCombinationApiService;

	@Override
	public InfBfsPayVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "selectById", "主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		//TODO 业务逻辑
		InfBfsPayDTO dto = this.bFSApiService.selectById(id);//this.自定义ApiService.selectById(id);
		return InfBfsPayVOTransform.toVO(dto);
	}

	@Override
	public Long add(InfBfsPayVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "add", "新增对象不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		//return this.infBfsPayApiService.add(InfBfsPayVOTransform.toDTO(entity));
		//TODO 业务逻辑
		return null;
	}

	@Override
	public boolean removeById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "removeById", "删除主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		//return this.infBfsPayApiService.removeById(id);
		//TODO 业务逻辑
		return false;
	}
	
	/**
     * 删除
     * @param ids
     * @return
     */
    public boolean removeByIds(List<Long> ids)throws BaseException{
    	if(CollectionUtils.isEmpty(ids)){
    		BaseLogUtils.info(logger, "removeByIds", "删除主键参数集合不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		};
		return false;
    }

	@Override
	public boolean modify(InfBfsPayVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "modify", "修改对象参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		//return this.infBfsPayApiService.modify(InfBfsPayVOTransform.toDTO(entity));
		MessageDTO<String> res = this.bFSApiService.paySuccess(InfBfsPayVOTransform.toDTO(entity));
		return res.isSuccess();
		//TODO 业务逻辑
//		return false;
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException{
		if(null == page) return new Pager();
		return this.bFSApiService.findPaysByPager(page);
	}

	@Override
	public List<InfBfsPayVO> searchList(InfBfsPayQueryCondition condition) throws BaseException{
		if(null == condition) return new ArrayList<InfBfsPayVO>();
		List<InfBfsPayDTO> list = this.bFSApiService.findPays(condition);
		return InfBfsPayVOTransform.toVOList(list);
	};
	@Override
	public JSONArray searchOrgTreeByCurrentUser(String currentBuOrgCd, String currentSysCd) throws BaseException {
		JSONArray jsonArray = new JSONArray ();
		SysOrgDTO sysOrgDto  = null;
		if(StringUtils.isNotBlank(currentBuOrgCd)){
			List<SysOrgDTO> orgList  = null;
			sysOrgDto = 	this.sysOrgApiService.selectSysOrgByOrgCd(currentBuOrgCd,currentSysCd);
			List<Long> orgId = new ArrayList<Long>();
			orgId.add(sysOrgDto.getId());
			if(null!=sysOrgDto){
				orgList = this.sysOrgApiService.searchRecuOrgWithchildBy(orgId, currentSysCd);
			}
			if(CollectionUtils.isEmpty(orgList)){
				return jsonArray;
			}
			for (SysOrgDTO sysOrgDTO : orgList) {
				JSONObject obj = new JSONObject();
				obj.put("ischecked", false);
				obj.put("id", sysOrgDTO.getId());
				obj.put("text", sysOrgDTO.getOrgNm());
				obj.put("pid", sysOrgDTO.getOrgPid());
				obj.put("orgCd", sysOrgDTO.getOrgCd());
				obj.put("orgDispNm", sysOrgDTO.getOrgDispNm());
				jsonArray.add(obj);
			}
		}
		JSONArray treeList = this.treeList(jsonArray,sysOrgDto.getOrgPid());
		return treeList;
	}
	/**
	 * 递归添加整个集合数据
	 * @param treeList
	 * @param parentId
	 * @return
	 */
	private JSONArray treeList(JSONArray treeList, Long parentId) {
		JSONArray childMenu = new JSONArray();
		for (Object object : treeList) {
			JSONObject jsonMenu = JSONObject.parseObject(JSON.toJSONString(object));
			long oid = jsonMenu.getLong("id");
			long pid = jsonMenu.getLong("pid");
			if (parentId == pid) {
				JSONArray c_node = treeList(treeList, oid);
				jsonMenu.put("children", c_node);
				childMenu.add(jsonMenu);
			}
		}
		return childMenu;
	}

	@Override
	public String searchOrgNm(String currentBuOrgCd, String currentSysCd) throws BaseException {
		String orgnm = "";
		if(StringUtils.isNotBlank(currentBuOrgCd)){
			SysOrgDTO sysOrgDto = 	this.sysOrgApiService.selectSysOrgByOrgCd(currentBuOrgCd,currentSysCd);
			if(sysOrgDto != null){
				orgnm = sysOrgDto.getOrgDispNm();
			}
		}
		return orgnm;
	}

	@Override
	public MessageDTO<String> manualSendPay(Long id) {
		MessageDTO<String> messagedto = this.bFSApiService.manualSendPay(id);
		return messagedto;
	}
	
	@Override
	public MessageDTO<String> payFailure(Long id) {
		InfBfsPayDTO payDto = new InfBfsPayDTO();
		payDto.setId(Long.valueOf(id)); //流水号
		payDto.setMessage("测试手动制造失败");
		MessageDTO<String> messagedto = this.bFSApiService.rejectUpdate(payDto);
		return messagedto;
	}
	@Override
	public Long[] downPaymentAmount(String paylistCode) throws BaseException {
		if (StringUtils.isEmail(paylistCode)) {
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "支付表号为空");
		}
		Long[] amountPayable = new Long[2];
		Long firstAmt = 0L;
		Long totalAmt = 0L;
		java.math.BigDecimal[] payAmt = this.insOrPrjCombinationApiService.amountOfPrice(paylistCode);
		for (int i = 0; i < payAmt.length; i++) {
			firstAmt = payAmt[0].longValue();
			totalAmt = payAmt[1].longValue();
		}
		amountPayable[0] = firstAmt;
		amountPayable[1] = totalAmt;
		return amountPayable;
	}
}
